import api from '../../api/api'
import {getUserInfo, updateUser, uploadavatar} from "../../api/conf";
import {base64Img } from "../../libs/base64src";
const myDate = new Date();

Page({
    data: {
        userId: 0,
        avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
        nickName: '微信用户',
        phoneNumber: '未绑定',
        gender: '请选择',
        genderVisible: false,
        birthDateVisible: false,
        date: myDate.getTime(),
        start: '1930-01-01',
        end: myDate.getTime(),
        birthDate: '请选择',
        type: null,
        genderOptions: [
            { label: '男',value: '男'},
            { label: '女',value: '女'},
        ],
    },
    onLoad() {
        this.getUserInfo()
    },

    getUserInfo: function (){
        wx.showLoading({
            title: '加载中',
            mask: true
        })
        wx.showNavigationBarLoading()
        api.get(getUserInfo, {
        }).then(data => {
            this.setData({
                userId: data.Data.Id,
                avatarUrl: data.Data.AvatarUrl?data.Data.AvatarUrl:this.data.avatarUrl,
                nickName: data.Data.Nickname?data.Data.Nickname:'微信用户',
                phoneNumber: data.Data.Phone?data.Data.Phone:'未绑定',
                gender: data.Data.Gender?data.Data.Gender:'请选择',
                birthDate: data.Data.BirthDate?data.Data.BirthDate:'请选择',
            })
            wx.hideNavigationBarLoading()
            wx.hideLoading()
        })
    },
    onChooseAvatar(e) {
        const { avatarUrl } = e.detail
        this.setData({
            avatarUrl,
        })
        base64Img(avatarUrl, 'png').then(data => {
            api.post(uploadavatar, {
                "base64Str": data
            }).then((res) => {
                wx.setStorageSync('avatarUrl', res.Data.AvatarUrl)
            })
        })
    },

    onClickCell({ currentTarget }) {
        const nickName = this.data.nickName;
        const { type } = currentTarget.dataset;
        switch (type) {
            case 'gender' :
            case 'birthDate':
                this.setData({
                    type,
                    [`${type}Visible`]: true,
                });
                break;
            case 'nickName':
                wx.navigateTo({
                    url: `/pages/user/name-edit/index?name=${nickName}`,
                });
                break;
            default: {
                break;
            }
        }
    },
    getPhoneNumber (e) {
        console.log(e.detail.code)  // 动态令牌
        console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
    },

    onConfirm(e) {
        let { value } = e.detail;
        const { type } = e.currentTarget.dataset;
        if(Array.isArray(value)){
            value = value[0]
        }
        this.setData({
            [`${type}`]: value,
        });

        api.post(updateUser, {
            model:{
                'Gender':this.data.gender,
                'BirthDate':this.data.birthDate,
            }
        }).then(data => {})
    },
});
